import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminReports extends GeneralReports {

	public AdminReports(Connection initConnection, DepositDBManager depositDBManager, CustomerDBManager customerDBManager, AccountDBManager accountDBManager, BalanceDBManager balanceDBManager, DepositTypesDBManager depositTypesDBManager) {
		super(initConnection, depositDBManager, customerDBManager, accountDBManager, balanceDBManager, depositTypesDBManager);
	}

	// List of all customers
	public List getListCustomers() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();

		String query = ("SELECT cust_id FROM customers ");

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long custId = resultSet.getLong("cust_id");
				Customer customer = customerDBManager.getCustomerById(custId);
				listReports.add(customer);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			System.out.println("[AdminRepors] All customers was added to report list");
		} else
			System.err.println("[AdminRepors] Customers wasn't added to report list");

		return listReports;
	}

	// List of all accounts
	public List getListAccounts() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();

		String query = ("SELECT account_id FROM accounts ");

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long custId = resultSet.getLong("account_id");
				Account account = accountDBManager.getAccountById(custId);
				listReports.add(account);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			System.out.println("[AdminRepors] All accounts was added to report list");
		} else
			System.err.println("[AdminRepors] Accounts wasn't added to report list");

		return listReports;
	}

	// List of customers by type of
	public List getListCustomersByType(long rank) throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();
		String rank_name = null;

		String query = ("SELECT  cust_id FROM customers WHERE rank = " + rank);

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long custId = resultSet.getLong("cust_id");
				Customer customer = customerDBManager.getCustomerById(custId);
				listReports.add(customer);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		query = ("SELECT  rank_name FROM rank_types WHERE rank = " + rank);
		try {

			resultSet = statement.executeQuery(query);
			resultSet.next();
			rank_name = resultSet.getString("rank_name");
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (flag) {
			System.out.println("[AdminRepors] All customers was added to report list by type :" + rank_name);
		} else
			System.err.println("[AdminRepors] Customers wasn't added to report list");

		return listReports;
	}

	public List getListDeposits() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();

		String query = ("SELECT deposit_id FROM deposits ");

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long deposit_id = resultSet.getLong("deposit_id");
				System.out.println(deposit_id);
				Deposit deposit = depositDBManager.getDepositById(deposit_id);
				listReports.add(deposit);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			System.out.println("[AdminRepors] All deposits was added to report list");
		} else
			System.err.println("[AdminRepors] Deposits wasn't added to report list");

		return listReports;
	}

	// Get List Of Deposit Types

	public List getListDepositTypes() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();

		String query = ("SELECT deposit_type FROM deposit_types ");

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long deposit_type = resultSet.getLong("deposit_type");
				DepositType type = depositTypesDBManager.getDepositTypesObject(deposit_type);
				listReports.add(type);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			System.out.println("[AdminRepors] All DepositTypes was added to report list");
		} else
			System.err.println("[AdminRepors] DepositType wasn't added to report list");

		return listReports;
	}

	// Get DepositType
	// Get List Of Deposit Types

	public DepositType getDepositType(long typeDeposit) throws DBManagerException {
		DepositType depositType = depositTypesDBManager.getDepositTypesObject(typeDeposit);
		return depositType;

	}

	// Get List Of actions MBank's Balance
	// Get List Of Deposit Types

	public List getListBalanceActionTypes() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();

		String query = ("SELECT balance_id FROM balance ");

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long balance_id = resultSet.getLong("balance_id");
				Balance balance = balanceDBManager.getBalanceById(balance_id);
				listReports.add(balance_id);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (flag) {
			System.out.println("[AdminRepors] All Balance's action was added to report list");
		} else
			System.err.println("[AdminRepors] Balance's action wasn't added to report list");

		return listReports;
	}

	// Get List Of actions MBank's Balance
	// Get List Of Deposit Types

	// resume of Balance's income money
	public List getResumeOfIcomengMoneyBalance() throws DBManagerException {

		listReports = new ArrayList();
		boolean flag = false;
		Connection conn = getConnection();
		int action = 1;// this action of incoming money on balance's MBank
		long sumMoney = 0;
		//
		String query = ("SELECT balance_id FROM balance WHERE action =  " + action);

		try {
			statement = conn.createStatement();
			resultSet = statement.executeQuery(query);

			while (resultSet.next()) {
				long balance_id = resultSet.getLong("balance_id");
				Balance balance = balanceDBManager.getBalanceById(balance_id);
				sumMoney += balance.getAmount();
				listReports.add(balance);

			}
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (flag) {
			System.out.println("[AdminRepors] All Balance's resume of incoming money was added to report list/n Sum of incoming money is " + sumMoney);
		} else
			System.err.println("[AdminRepors] Balance's resume of incoming money wasn't added to report list");

		return listReports;
	}

}
